package com.wuhao.springboot.service.impl;

import java.lang.reflect.InvocationTargetException;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuhao.springboot.entity.Order;
import com.wuhao.springboot.entity.RepairTask;
import com.wuhao.springboot.entity.User;
import com.wuhao.springboot.entity.dto.RepairTaskDTO;
import com.wuhao.springboot.entity.ro.PageRO;
import com.wuhao.springboot.entity.ro.RepairPageRO;
import com.wuhao.springboot.entity.vo.RepairTaskPageVO;
import com.wuhao.springboot.mapper.OrderMapper;
import com.wuhao.springboot.mapper.RepairTaskMapper;
import com.wuhao.springboot.service.RepairTaskService;
import com.wuhao.springboot.type.StatusEnum;
import com.wuhao.springboot.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description
 * @Date 2023/2/1 22:18
 * @Author pq
 */
@Slf4j
@Service
public class RepairTaskServiceImpl extends ServiceImpl<RepairTaskMapper, RepairTask> implements RepairTaskService {

    @Resource
    private RepairTaskMapper repairTaskMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public void addRepairTask(RepairTask ro) {
        this.save(ro);
    }

    @Override
    public void updateRepairTask(RepairTask ro) {
        this.updateById(ro);
    }

    @Override
    public void delete(RepairTask ro) {
        this.delete(ro);
    }

    @Override
    public RepairTaskPageVO pageAll(RepairPageRO ro) {
        LambdaQueryWrapper<RepairTask> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<RepairTask> repairTaskPage = new Page<>(ro.getCurrent(), ro.getSize());
        IPage<RepairTask> repairTaskIPage = repairTaskMapper.selectPage(repairTaskPage, userLambdaQueryWrapper);
        List<RepairTaskDTO> repairTaskDTOS = Lists.newArrayList();
        repairTaskIPage.getRecords().forEach(repairTask -> {
            RepairTaskDTO dto = new RepairTaskDTO();
            dto.setId(repairTask.getId());
            dto.setOrderId(repairTask.getOrderId());
            dto.setAppointTime(repairTask.getAppointTime());
            dto.setStatus(repairTask.getStatus().getDesc());
            dto.setRepairerId(repairTask.getRepairerId());
            dto.setRepairName(repairTask.getRepairName());
            dto.setAirCondType(repairTask.getAirCondType());
            dto.setAirCondCode(repairTask.getAirCondCode());
            dto.setSpeculateReason(repairTask.getSpeculateReason());
            dto.setRepairReco(repairTask.getRepairReco());
            dto.setScore(repairTask.getScore());
            repairTaskDTOS.add(dto);
        });
        RepairTaskPageVO vo = new RepairTaskPageVO();
        vo.setRecords(repairTaskDTOS);
        vo.setPages(repairTaskIPage.getPages());
        vo.setTotal(repairTaskIPage.getTotal());
        return vo;
    }

    @Override
    public RepairTaskPageVO pageMyTask(RepairPageRO ro) {
        QueryWrapper<RepairTask> userLambdaQueryWrapper = new QueryWrapper<>();
        User currentUser = TokenUtils.getCurrentUser();
        userLambdaQueryWrapper.eq("repairer_id",currentUser.getId());
        Page<RepairTask> repairTaskPage = new Page<>(ro.getCurrent(), ro.getSize());
        IPage<RepairTask> repairTaskIPage = repairTaskMapper.selectPage(repairTaskPage, userLambdaQueryWrapper);
        List<RepairTaskDTO> repairTaskDTOS = Lists.newArrayList();
        repairTaskIPage.getRecords().forEach(repairTask -> {
            RepairTaskDTO dto = new RepairTaskDTO();
            dto.setId(repairTask.getId());
            dto.setOrderId(repairTask.getOrderId());
            dto.setAppointTime(repairTask.getAppointTime());
            dto.setStatus(repairTask.getStatus().getDesc());
            dto.setRepairerId(repairTask.getRepairerId());
            dto.setRepairName(repairTask.getRepairName());
            dto.setAirCondType(repairTask.getAirCondType());
            dto.setAirCondCode(repairTask.getAirCondCode());
            dto.setSpeculateReason(repairTask.getSpeculateReason());
            dto.setRepairReco(repairTask.getRepairReco());
            dto.setScore(repairTask.getScore());
            repairTaskDTOS.add(dto);
        });
        RepairTaskPageVO vo = new RepairTaskPageVO();
        vo.setRecords(repairTaskDTOS);
        vo.setPages(repairTaskIPage.getPages());
        vo.setTotal(repairTaskIPage.getTotal());
        return vo;
    }

    @Override
    public void finish(RepairTask ro) {
        RepairTask repairTask = repairTaskMapper.selectById(ro.getId());
        repairTask.setStatus(StatusEnum.FINISHED);
        this.updateById(repairTask);
        log.info("任务状态修改完成{}",repairTask);
        Order order=orderMapper.selectById(repairTask.getOrderId());
        order.setStatus(StatusEnum.FINISHED);
        orderMapper.updateById(order);
        log.info("订单状态修改完成{}",repairTask);
    }

    @Override
    public Page<RepairTaskDTO> page(PageRO ro) {
        QueryWrapper<RepairTask> queryWrapper=new QueryWrapper<>();
        List<RepairTaskDTO> repairTaskDTOS=Lists.newArrayList();
        Page<RepairTask> page = this.page(new Page<>(ro.getCurrent(), ro.getSize()), queryWrapper);
        page.getRecords().forEach(task->{
            RepairTaskDTO dto=new RepairTaskDTO();
            try {
                BeanUtils.copyProperties(dto,task);
                dto.setStatus(task.getStatus().getDesc());
                repairTaskDTOS.add(dto);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        });
        Page<RepairTaskDTO> repairTaskPage=new Page<>();
        repairTaskPage.setPages(page.getPages());
        repairTaskPage.setRecords(repairTaskDTOS);
        repairTaskPage.setTotal(page.getTotal());
        return repairTaskPage;
    }
}
